
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// Member_WithdrawAccount Data Class
    /// </summary>
    public partial class Member_WithdrawAccountDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Member_WithdrawAccount";

        public Member_WithdrawAccountDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Member_WithdrawAccount model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [Member_WithdrawAccount](");
            strSql.Append("[AccountID],[MemberAccount],[BankName],[BankAccountNo],[BankAccountName],[BankPosition],[BankCountry],[IsDefault],[Memo],[CreateTime],BankID,BankCity,BankProvince");
            strSql.Append(") values (");
            strSql.Append("@AccountID,@MemberAccount,@BankName,@BankAccountNo,@BankAccountName,@BankPosition,@BankCountry,@IsDefault,@Memo,@CreateTime,@BankID,@BankCity,@BankProvince");
            strSql.Append(")");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@AccountID", model.AccountID),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@BankName", model.BankName),
                    new SqlParameter("@BankAccountNo", model.BankAccountNo),
                    new SqlParameter("@BankAccountName", model.BankAccountName),
                    new SqlParameter("@BankPosition", model.BankPosition),
                    new SqlParameter("@BankCountry", model.BankCountry),
                    new SqlParameter("@IsDefault", model.IsDefault),
                    new SqlParameter("@Memo", model.Memo),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@BankID", model.BankID),
                    new SqlParameter("@BankCity", model.BankCity),
                    new SqlParameter("@BankProvince", model.BankProvince)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Member_WithdrawAccount model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Member_WithdrawAccount] set ");
            strSql.Append("[MemberAccount]=@MemberAccount,BankID=@BankID,BankCity=@BankCity,BankProvince=@BankProvince,[BankName]=@BankName,[BankAccountNo]=@BankAccountNo,[BankAccountName]=@BankAccountName,[BankPosition]=@BankPosition,[BankCountry]=@BankCountry,[IsDefault]=@IsDefault,[Memo]=@Memo,[CreateTime]=@CreateTime");
            strSql.Append(" where [AccountID]=@AccountID");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@AccountID", model.AccountID),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@BankName", model.BankName),
                    new SqlParameter("@BankAccountNo", model.BankAccountNo),
                    new SqlParameter("@BankAccountName", model.BankAccountName),
                    new SqlParameter("@BankPosition", model.BankPosition),
                    new SqlParameter("@BankCountry", model.BankCountry),
                    new SqlParameter("@IsDefault", model.IsDefault),
                    new SqlParameter("@Memo", model.Memo),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@BankID", model.BankID),
                    new SqlParameter("@BankCity", model.BankCity),
                    new SqlParameter("@BankProvince", model.BankProvince)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        ///  设默认银行卡
        /// </summary>
        public bool SetDefault(Member_WithdrawAccount model)
        {
            List<SqlAndParams> dict = new List<SqlAndParams>();

            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("update [Member_WithdrawAccount] set ");
            strSql1.Append("[IsDefault]=0");
            strSql1.Append(" where MemberAccount=@MemberAccount");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@MemberAccount", model.MemberAccount)
            };
            dict.Add(new SqlAndParams() { Sql = strSql1.ToString(), SqlParameters = parameters1 });

            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Member_WithdrawAccount] set ");
            strSql.Append("[IsDefault]=@IsDefault");
            strSql.Append(" where [AccountID]=@AccountID");

            SqlParameter[] parameters = {
                    new SqlParameter("@IsDefault", model.IsDefault),
                     new SqlParameter("@AccountID", model.AccountID)
            };
            dict.Add(new SqlAndParams() { Sql = strSql.ToString(), SqlParameters = parameters });



            return DbHelperSQL.ExecuteSqlTran(dict);
        }
        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Guid AccountID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Member_WithdrawAccount] ");
            strSql.Append(" where [AccountID]=@AccountID");

            SqlParameter[] parameters = {
			
                new SqlParameter("@AccountID", AccountID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_WithdrawAccount Get(Guid AccountID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [Member_WithdrawAccount] ");
            strSql.Append(" where [AccountID]=@AccountID");

            SqlParameter[] parameters = {
			
                new SqlParameter("AccountID", AccountID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_WithdrawAccount Get(DataRow row)
        {
            if (row == null) return null;

            Member_WithdrawAccount model = new Member_WithdrawAccount();


            if (row["AccountID"] != DBNull.Value)
            {
                model.AccountID = (Guid)row["AccountID"];
            }

            if (row["MemberAccount"] != DBNull.Value)
            {
                model.MemberAccount = (String)row["MemberAccount"];
            }

            if (row["BankName"] != DBNull.Value)
            {
                model.BankName = (String)row["BankName"];
            }

            if (row["BankAccountNo"] != DBNull.Value)
            {
                model.BankAccountNo = (String)row["BankAccountNo"];
            }

            if (row["BankAccountName"] != DBNull.Value)
            {
                model.BankAccountName = (String)row["BankAccountName"];
            }

            if (row["BankPosition"] != DBNull.Value)
            {
                model.BankPosition = (String)row["BankPosition"];
            }

            if (row["BankCountry"] != DBNull.Value)
            {
                model.BankCountry = (String)row["BankCountry"];
            }

            if (row["IsDefault"] != DBNull.Value)
            {
                model.IsDefault = (Boolean)row["IsDefault"];
            }

            if (row["Memo"] != DBNull.Value)
            {
                model.Memo = (String)row["Memo"];
            }

            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }

            if (row["BankID"] != DBNull.Value)
            {
                model.BankID = (String)row["BankID"];
            }
            if (row["BankProvince"] != DBNull.Value)
            {
                model.BankProvince = (String)row["BankProvince"];
            }
            if (row["BankCity"] != DBNull.Value)
            {
                model.BankCity = (String)row["BankCity"];
            }
            
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [Member_WithdrawAccount] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Member_WithdrawAccount] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Member_WithdrawAccount] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Member_WithdrawAccount]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by createTime desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        /// <summary>
        /// 获取个人银行账户
        /// </summary>
        /// <returns></returns>
        public DataTable GetMyCorporateAccounts(string MemberAccount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM [Member_WithdrawAccount] ");
            if (MemberAccount.Trim() != "")
            {
                strSql.Append(string.Format(" WHERE MemberAccount = '{0}'", MemberAccount));
            }
            strSql.Append(" ORDER BY CreateTime DESC ");

            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string memberAccount, string bankName, string bankAccountName, string BankAccountNo)
        {
            string sql = "select count(1) from Member_WithdrawAccount where memberAccount = @memberAccount and bankName = @bankName and bankAccountName = @bankAccountName and BankAccountNo = @BankAccountNo";

            SqlParameter[] parameters = {
					new SqlParameter("@memberAccount", memberAccount),	
                    new SqlParameter("@bankName", bankName),
                    new SqlParameter("@bankAccountName", bankAccountName),
                    new SqlParameter("@BankAccountNo", BankAccountNo)
										};

            return DbHelperSQL.Exists(sql, parameters);
        }
    }
}


